package org.example.maven.controller;

import jakarta.transaction.Transactional;
import org.example.maven.pojo.Result;
import org.example.maven.pojo.exhibition.list.AddExhibition;
import org.example.maven.pojo.exhibition.list.SearchExhibition;
import org.example.maven.pojo.exhibition.list.ticket.ExhibitionListTicket;
import org.example.maven.service.ExhibitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@CrossOrigin
@RequestMapping("/exhibition")
public class ExhibitionController {
    @Autowired
    private ExhibitionService exhibitionService;
    @PreAuthorize("permitAll()")
    @GetMapping
    //展会列表
    public Result getAllExhibition(@RequestParam("pageNum")int pageNum,@RequestParam("pageSize") int pageSize){
        Result res=new Result();
        res.put("code",200);
        res.put("data",exhibitionService.getAllExhibition(pageNum,pageSize));
        res.put("total",exhibitionService.getExhibitionCount(new SearchExhibition()));
        res.put("message","ok");
        return res;
    }
    @PostMapping
    @Transactional
    //新增展会
    public Result addExhibition(@RequestBody AddExhibition addExhibition){
        addExhibition.setCreatedAt(LocalDateTime.now());
        exhibitionService.addExhibition(addExhibition);
        System.out.println(addExhibition);
        exhibitionService.setExhibitionCategory(addExhibition.getId(),addExhibition.getExhiClass());
        return Result.success("新增成功");
    }

    @GetMapping("/{id}")
    @Transactional
    @PreAuthorize("permitAll()")
    //根据获取展会详情
    public Result getExhibitionById(@PathVariable int id){
        return Result.success(exhibitionService.getExhibitionById(id));
    }

    @PostMapping("/setExhibitionTicket")
    @Transactional
    @PreAuthorize("permitAll()")
    //设置展会票数
    public Result setExhibitionTicket(@RequestBody ExhibitionListTicket exhibitionListTicket){
        exhibitionService.setExhibitionTicket(exhibitionListTicket);
        //System.out.println(exhibitionListTicket);
        return Result.success("放票成功");
    }

    @GetMapping("/getExhibitionTicket/{id}")
    @Transactional
    @PreAuthorize("permitAll()")
    //获取展会票数
    public Result getExhibitionTicket(@PathVariable int id) throws ParseException {
        return Result.success(exhibitionService.getExhibitionTicket(id));
    }

    //获取对应展览的订单管理信息
    @GetMapping("/getExhibitionOrder/{id}")
    @Transactional
    @PreAuthorize("permitAll()")
    public Result getExhibitionOrder(@PathVariable int id){
        return Result.success(exhibitionService.getExhibitionOrderById(id));
    }
}
